{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "instruction_format = \"你是专门进行实体抽取的专家。请从text中抽取出符合schema定义的实体，不存在的实体类型返回空列表。请按照JSON字符串的格式回答。, 'schema': {schema}, 'text': '{text}'\"\n",
    "\n",
    "def convert_coccano2LLM(input_file, output_file, schema):\n",
    "\n",
    "    with open(input_file, 'r') as f:\n",
    "        with open(output_file, 'w') as w:\n",
    "            for line in f:\n",
    "                d = json.loads(line)\n",
    "                text = d['text']\n",
    "                ent_labels = d['label']\n",
    "\n",
    "                output = {}\n",
    "                for ent_class in schema:\n",
    "                    output[ent_class] = []\n",
    "\n",
    "                for ent in ent_labels:\n",
    "                    start, end, ent_class = ent\n",
    "                    output[ent_class].append(text[start:end])\n",
    "                \n",
    "                llm_data = {\n",
    "                    'instruction':instruction_format.format(schema=schema, text=text),\n",
    "                    \"input\":\"\",\n",
    "                    \"output\": json.dumps(output, ensure_ascii=False)\n",
    "                }\n",
    "\n",
    "                w.write(json.dumps(llm_data, ensure_ascii=False) + '\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "convert_coccano2LLM(\n",
    "    'data/guihua_10.jsonl', \n",
    "    'data/llm_10.jsonl', \n",
    "    schema=['重要项目', '功能定位', '主要任务','基本遵循','关键数据','数值']\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<!-- label': [[21, 29, '重要项目']] -->"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "llm",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.14"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
